import router from './router'

// 引入vuex
import store from '@/store'

// 网站顶部进度条效果
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style

NProgress.configure({ showSpinner: false }) // NProgress Configuration

const whiteList = ['/login', '/404'] // no redirect whitelist

// 路由前置守卫
router.beforeEach(async (to, from, next) => {
  // 开始路由跳转，开启进度条
  NProgress.start()
  // 首先判断有没有token
  if (store.getters.token) {
    // 判断用户要去login页面直接跳转到主页
    if (to.path === '/login') {
      next('/')

      NProgress.done()
    } else {
      // 判断是否已经获取了用户信息
      if (!store.getters.userId) {
        await store.dispatch('user/storeGetUserInfo')
      }
      next()
    }
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
      NProgress.done()
    }
  }
})

// 路由后置守卫
router.afterEach(() => {
  NProgress.done()
})
